package com.well.register.event.job;

import com.alibaba.fastjson.JSONObject;
import com.well.register.console.service.JobExecutorService;
import com.well.register.event.AppEvent;
import com.well.rpc.model.DoJobResponseModel;
import com.well.rpc.model.EventType;
import com.well.rpc.model.TransData;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * <p>
 * do job resp event
 * </p>
 *
 * @author weedo
 * @since 2022/8/21
 */

@Slf4j
@Component
public class DoJobResponseEvent implements AppEvent {

    @Resource
    private JobExecutorService jobExecutorService;

    @Override
    public void doEvent(ChannelHandlerContext channelHandlerContext, TransData transData) {
        DoJobResponseModel responseModel = getDoJobResponseModel(transData);
        jobExecutorService.saveExecutorResult(responseModel);
    }

    @Override
    public EventType eventType() {
        return EventType.DO_JOB_RESPONSE;
    }

    private DoJobResponseModel getDoJobResponseModel(TransData transData) {
        return JSONObject.parseObject(transData.getJsonData(), DoJobResponseModel.class);
    }
}
